package com.company;


import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;

public class Pole {

	private int i, j;

	private boolean isOpen;                 // czy otwarta

	private boolean isMine;  //czy jest mina
	private Plansza plansza;          // przekazanie planszy
	private JButton button;

	public Pole( int i, int j, Plansza plansza ) {
		this.isOpen = false;
		this.isMine = false;
		this.i = i;
		this.j = j;
		this.plansza = plansza;
		button = new JButton();
		button.addMouseListener( new MouseInputListener() {

			@Override
			public void mouseClicked( MouseEvent e ) {
				open();
			}

			@Override
			public void mousePressed( MouseEvent e ) {

			}

			@Override
			public void mouseReleased( MouseEvent e ) {

			}

			@Override
			public void mouseEntered( MouseEvent e ) {

			}

			@Override
			public void mouseExited( MouseEvent e ) {

			}

			@Override
			public void mouseDragged( MouseEvent e ) {

			}

			@Override
			public void mouseMoved( MouseEvent e ) {

			}
		} );


//		plansza.add( button );
	}

	int mineCount() {
		int c = 0;
		if ( plansza.isMine( i - 1, j - 1 ) ) {
			c++;
		}
		if ( plansza.isMine( i, j - 1 ) ) {
			c++;
		}
		if ( plansza.isMine( i + 1, j - 1 ) ) {
			c++;
		}
		if ( plansza.isMine( i - 1, j ) ) {
			c++;
		}
		if ( plansza.isMine( i - 1, j + 1 ) ) {
			c++;
		}
		if ( plansza.isMine( i, j + 1 ) ) {
			c++;
		}
		if ( plansza.isMine( i + 1, j + 1 ) ) {
			c++;
		}

		if ( plansza.isMine( i + 1, j ) ) {
			c++;
		}


		return c;
	}

	public void open() {
		if ( !isOpen ) {
			isOpen = true;
			button.setEnabled( false );
		}
		if ( isMine ) {
			JOptionPane.showMessageDialog( button, "Przegrales" );
			plansza.openAll();
			int n = JOptionPane.showConfirmDialog( button, "Czy chcesz zagrac ponownie?", "",
					JOptionPane.YES_NO_OPTION );
			if ( n == JOptionPane.NO_OPTION ) {
				System.exit( 0 );
			} else {

				plansza.initializeNewGame();

			}

		} else {
			int mc = mineCount();
			if ( mc > 0 ) {
				button.setText( String.valueOf( mc ) );
			} else {

				if ( i - 1 >= 0 ) {                                     //rekurencyjnie odslania wszystkie pola ograniczone polami sasiadujacymi z minami
					plansza.getFieldAtIndex( i - 1, j ).autoOpen();
				}
				if ( i + 1 < plansza.getN() ) {
					plansza.getFieldAtIndex( i + 1, j ).autoOpen();
				}
				if ( j - 1 >= 0 ) {
					plansza.getFieldAtIndex( i, j - 1 ).autoOpen();
				}
				if ( j + 1 < plansza.getN() ) {
					plansza.getFieldAtIndex( i, j + 1 ).autoOpen();
				}


			}

		}
	}

	public void autoOpen() {                //pokazanie pol z pominieciem pol z minami i juz pokazanych
		if ( !isMine && !isOpen ) {
			open();
		}
	}

	public boolean isMine() {
		return isMine;
	}

	public void setMine( boolean mine ) {
		isMine = mine;
	}

	public int getI() {
		return i;
	}

	public void setI( int i ) {
		this.i = i;
	}

	public int getJ() {
		return j;
	}

	public void setJ( int j ) {
		this.j = j;
	}

	public boolean isOpen() {
		return isOpen;
	}

	public void setOpen( boolean open ) {
		isOpen = open;
	}

	public Plansza getPlansza() {
		return plansza;
	}

	public void setPlansza( Plansza plansza ) {
		this.plansza = plansza;
	}

	public JButton getButton() {
		return button;
	}

	public void setButton( JButton button ) {
		this.button = button;
	}
}

